package zxh.com.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import zxh.com.model.FsFile;
import zxh.com.service.FsFileService;
import zxh.com.until.UploadFile;
 

 

@Controller
@RequestMapping("/file")
public class FileController {
	@Resource
    private FsFileService fileService;
    @ResponseBody
    @RequestMapping("upfile")
    public Object upfile(@RequestParam("file")MultipartFile file, Model model, HttpServletRequest request,HttpSession session,FsFile n)
    {	
    	 n=UploadFile.upfile(file, request );
    	 fileService.insert(n);
    	 return n;
    }
    @ResponseBody
    @RequestMapping("list")
    public Object list(  Model model, HttpServletRequest request,HttpSession session,FsFile n)
    {
    	 List<FsFile> nli=fileService.list(n);
    	 return nli;
    }
    @RequestMapping(value = "/downfile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String  downfile(HttpSession session,HttpServletResponse response,FsFile o,HttpServletRequest request) throws Exception {
    	  try {
		 o=fileService.selectById(o.getId());
	      //  String abPath= request.getSession().getServletContext().getRealPath(o.getFpath())  ;
	        String abPath= o.getFpath();
			abPath=abPath.replace("/", "\\");
	        String filepath = abPath;
		        // path是指想要下载的文件的路径
		        File file = new File(filepath);
		        // 将文件写入输入流
		        FileInputStream fileInputStream = new FileInputStream(file);
		        InputStream fis = new BufferedInputStream(fileInputStream);
		        byte[] buffer = new byte[fis.available()];
		        fis.read(buffer);
		        fis.close();
		        // 清空response
		        response.reset();
		        // 设置response的Header
		        response.setCharacterEncoding("UTF-8");
		        //Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
		        //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
		        // filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
		        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(o.getOldname(), "UTF-8"));
		        // 告知浏览器文件的大小
		        response.addHeader("Content-Length", "" + file.length());
		        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
		        response.setContentType("application/octet-stream");
		        outputStream.write(buffer);
		        outputStream.flush();
		    } catch (Exception ex) {
		        ex.printStackTrace();
		        return "401";
		    }
		  return "1";
    }
}
